package cc.willrock.algorithm.other;

import java.util.Arrays;

public class HornerRule {

	/**
	 * @param args
	 * http://flynoi.blog.hexun.com/31272178_d.html
	 * 
	 * 
	 * p(x)=2x4－x3＋3x2＋x－5

	 *	=x(2x3－x2＋3x＋1)－5
		
	 *	=x(x(2x2－x＋3)＋1)－5
		
	 *	=x(x(x(2x－1)＋3)＋1)－5
	 * 
	 */
	
	
	public static Long getResult(Long a[],int k ){
		int size = a.length;
		long horner=a[0];
		for(int i =1;i<a.length;i++){
			horner = horner*k+a[i];
		}
		
		return horner;
	}
	public static void main(String[] args) {
		Long a[] = {2L,-1L,3L,1L,-5L};
		
		int k =3;
		System.out.println(getResult(a,k));
	}

}
